/**
 * 循环获取消息
 */

#include "Looper.h"
#include "Handler.h"

Looper::Looper() : Looper(new MessageQueue(INT16_MAX)) {
  _QuitLoop = false;
}

Looper::Looper(MessageQueue *pMsgQueue) :
    _pMsgQueue(pMsgQueue) {

}

Looper::~Looper() noexcept {
  DELETE_PTR(_pMsgQueue)
}

void Looper::loop() {
  while (!_QuitLoop) {
    auto pMsg = _pMsgQueue->dequeueMessage(0);
    if (pMsg) {
      pMsg->pHandler->dispatchMessage(pMsg);
      if (pMsg->what == MsgID_Release) {
        _pMsgQueue->abort();
        _QuitLoop = true;
      }
    }
  }
}

MessageQueue *Looper::getMessageQueue() {
  return _pMsgQueue;
}
